﻿//
// CreateUserViewModel.cs
//
// Authors:
//    Anders Gade Nørskov <09549@iha.dk
//    Lars Rønde Hansen <09548@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace RSD.ViewModel
{
    using Data;
    using Messages;

    public class CreateUserViewModel : ViewModelBase
    {
        private readonly IUserGroupRepository userGroupRepository;
        private readonly IUserRepository userRepository;
        private readonly RelayCommand createUserCommand;

        private string username;
        private string password;
        private string firstname;
        private string lastname;

        public CreateUserViewModel(IUserRepository userRepository, IUserGroupRepository userGroupRepository)
        {
            this.userRepository = userRepository;
            this.userGroupRepository = userGroupRepository;

            this.UserGroups = new ObservableCollection<UserGroup>(
                userGroupRepository.FindAll());
        
            this.createUserCommand = new RelayCommand(CreateUserExecute);
        }

        public string LastName
        {
            get
            {
                return lastname;
            }
            set
            {
                lastname = value;
                RaisePropertyChanged("LastName");
            }
        }

        public string FirstName
        {
            get
            {
                return firstname;
            }
            set
            {
                firstname = value;
                RaisePropertyChanged("FirstName");
            }
        }

        public string UserName
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
                RaisePropertyChanged("UserName");
            }
        }

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        public ObservableCollection<UserGroup> UserGroups
        {
            get;
            set;
        }

        private void CreateUserExecute()
        {
            var user = userRepository.GenerateUser(username, password, 
                string.Format("{0} {1}", firstname, lastname));

            Messenger.Default.Send(new UserCreatedMessage(user));
        }
    }
}
